package com.mlt.back.dao.impl;

import com.mlt.back.beans.BasicInfo;
import com.mlt.back.dao.BasicInfoDao;
import com.mlt.back.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 类目数据库访问操作
 *@author 李
 *@date 2022/12/28
*/
public class BasicInfoDaoImpl implements BasicInfoDao {

    @Override
    public int insert(BasicInfo basicInfo) {

        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn= DBUtil.getConnection();
            String sql="insert into t_basic_info(basic_info_name,basic_info_status) values(?,?)";
            ps=conn.prepareStatement(sql);
            ps.setString(1, basicInfo.getBasic_info_name());
            ps.setInt(2, basicInfo.getBasic_info_status());

            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,null);
        }
        return 0;

    }

    @Override
    public int delete(Integer id) {

        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn= DBUtil.getConnection();
            String sql="delete from t_basic_info where basic_info_id=?";
            ps=conn.prepareStatement(sql);
            ps.setInt(1, id);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,null);
        }
        return 0;

    }

    @Override
    public int update(BasicInfo basicInfo) {

        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn= DBUtil.getConnection();
            String sql="update t_basic_info set basic_info_name=?,basic_info_status=? where basic_info_id=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1, basicInfo.getBasic_info_name());
            ps.setInt(2, basicInfo.getBasic_info_status());
            ps.setInt(3, basicInfo.getBasic_info_id());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,null);
        }
        return 0;

    }

    @Override
    public List<BasicInfo> selectAll() {

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn= DBUtil.getConnection();
            String sql="select * from t_basic_info";
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();

            BasicInfo basicInfo = null;
            List<BasicInfo> basicInfos = new ArrayList<>();
            while (rs.next()){

                basicInfo = new BasicInfo();
                basicInfo.setBasic_info_id(rs.getInt("basic_info_id"));
                basicInfo.setBasic_info_name(rs.getString("basic_info_name"));
                basicInfo.setBasic_info_status(rs.getInt("basic_info_status"));

                basicInfos.add(basicInfo);

            }
            return basicInfos;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,rs);
        }
        return null;

    }

}

